//
//  CellForFocusNewsTop.m
//  世界
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForFocusNewsTop.h"
#import "ModelForFocusNews.h"
#import <UIImageView+WebCache.h>
#import "CellForFocusNewsTopRoll.h"
#import "ViewControllerForNewsDetails.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface CellForFocusNewsTop ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UILabel *lableForTitle;
@property (nonatomic, retain) UIImageView *imgViewForPicUrl;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, assign) BOOL isDragging;
@property (nonatomic, assign) NSInteger number;


@end

@implementation CellForFocusNewsTop





#pragma mark 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self != nil)
    {
        self.number= 1;
        
        [self createCollectionView];
        
        [self createPageView];
        
        [self setUpTimer];
    }
    return self;
}


- (void)createPageView {
    
    self.page = [[UIPageControl alloc] init];
    
    [self.contentView addSubview:self.page];
    
    self.page.backgroundColor = [UIColor clearColor];
    
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    
    [_page release];
}


-(void)changeImage
{
    if (self.number == self.arrModelForTop.count - 1)
    {
        self.number = 1;
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.number inSection:0];
        
        [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally   animated:NO];
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:++self.number inSection:0];
    
    [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally   animated:YES];
    
    switch (self.number)
    {
        case 1:
        case 4:
            self.page.currentPage = 0;
            break;
            
        case 2:
            self.page.currentPage = 1;
            break;
            
        case 3:
        case 0:
            self.page.currentPage = 2;
            break;
    }
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.collection.contentOffset.x == 0)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:3 inSection:0];
        
        [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally   animated:NO];
    }
    if (self.collection.contentOffset.x == (self.arrModelForTop.count - 1) * WIDTH)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
        
        [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally   animated:NO];
    }

     self.page.currentPage = self.collection.contentOffset.x / WIDTH - 1;
}






/** 自动轮播 **/
- (void)setUpTimer
{
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}


//移除定时器
- (void)removeTimer
{
    
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - 当用户开始退拽的时候调用移除计时器方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

#pragma mark -- 当用户停止拖拽时, 调用定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setUpTimer];

}






#pragma mark 布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collection.frame = self.contentView.frame;
    
    self.collection.contentOffset = CGPointMake(414, 0);
    
    self.page.frame = CGRectMake(360, 260, 50, 40);
    
    self.flowLayout.itemSize = CGSizeMake(self.contentView.frame.size.width, self.contentView.frame.size.height);
}





#pragma mark 自定义方法 >>>>>>
#pragma mark 设置布局格式
//FlowLayout父类是UIcollectionViewLayout
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //item之间最小间距(横向)
    self.flowLayout.minimumInteritemSpacing = 0;
    
    self.flowLayout.minimumLineSpacing = 0;
   
    //滑动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}




#pragma mark 创建CollectionView
- (void)createCollectionView
{
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.flowLayout];
    
    [_flowLayout release];
    
     self.collection.pagingEnabled = YES;
    
    self.collection.showsHorizontalScrollIndicator = NO;
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.showsVerticalScrollIndicator = NO;
    
    [self.contentView addSubview: self.collection];
    
    [_collection release];
    
    
    //指定代理人
    self.collection.dataSource = self;
    
    self.collection.delegate = self;
    
    //注册Cell(collectionView 只支持先注册)
    [self.collection registerClass:[CellForFocusNewsTopRoll class] forCellWithReuseIdentifier:@"pool1"];
    
     self.collection.contentOffset = CGPointMake(WIDTH, 0);
}



#pragma mark UICollectionViewDataSource 的协议方法 >>>>>>
#pragma mark 返回Item的个数 (required 1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrModelForTop.count;
}




#pragma mark 给相应的Item上添加Cell (required 2/2)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellForFocusNewsTopRoll *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    ModelForFocusNews *model = [self.arrModelForTop objectAtIndex:indexPath.item];
    
    cell.tag = indexPath.item;
    
    [cell handleData:model];
    
    return cell;
}




#pragma mark UICollectionViewDelegate 的协议方法 >>>>>>
#pragma mark 选择相应的Item所触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerForNewsDetails *vc = [[ViewControllerForNewsDetails alloc] init];
    
    ModelForFocusNews *model2 = [self.arrModelForTop objectAtIndex:indexPath.item];
    
    vc.strForNewsUrl = model2.NewsUrl;
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc1 = (UIViewController*)object;
    
    //1.设置self.tabBarController.tabBar.hidden=YES;
    vc1.tabBarController.tabBar.hidden = YES;
    
    //2.如果在push跳转时需要隐藏tabBar，设置self.hidesBottomBarWhenPushed=YES;
    vc1.hidesBottomBarWhenPushed=YES;
    
    [vc1.navigationController pushViewController:vc animated:YES];
    
    
    //3.在push后设置self.hidesBottomBarWhenPushed=NO,这样back回来的时候，tabBar会恢复正常显示。
    vc1.hidesBottomBarWhenPushed=NO;
}








- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
